package vip.xiaonuo.biz.modular.notice.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import vip.xiaonuo.auth.core.pojo.SaBaseLoginUser;
import vip.xiaonuo.auth.core.util.StpLoginUserUtil;
import vip.xiaonuo.biz.modular.group.entity.BizGroupUser;
import vip.xiaonuo.biz.modular.group.service.BizGroupUserService;
import vip.xiaonuo.biz.modular.notice.entity.BizNoticeList;
import vip.xiaonuo.biz.modular.notice.param.BizNoticeGetParam;
import vip.xiaonuo.biz.modular.notice.param.BizNoticeMessageParam;
import vip.xiaonuo.biz.modular.notice.service.BizNoticeService;
import vip.xiaonuo.biz.modular.org.entity.BizOrg;
import vip.xiaonuo.biz.modular.org.service.BizOrgService;
import vip.xiaonuo.biz.modular.user.entity.BizUser;
import vip.xiaonuo.biz.modular.user.service.BizUserService;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @author xsl
 * @date 2024-07-15 10:05
 **/
@Service
public class BizNoticeServiceImpl implements BizNoticeService {

    @Resource
    BizUserService bizUserService;

    @Resource
    BizOrgService bizOrgService;

    @Resource
    BizGroupUserService bizGroupUserService;

    @Override
    public List<BizNoticeList> getList(BizNoticeGetParam bizNoticeGetParam) {
        List<String> loginUserDataScope = StpLoginUserUtil.getLoginUserDataScope();
        SaBaseLoginUser user = StpLoginUserUtil.getLoginUser();
        bizNoticeGetParam.setUserId(user.getId());
        List<BizNoticeList> re =  new ArrayList<>();
        if(ObjectUtil.isNotEmpty(loginUserDataScope)) {
            if(bizNoticeGetParam.getSearchName() == null) bizNoticeGetParam.setSearchName("");
            // 查询所在机构
            if (Objects.equals(bizNoticeGetParam.getNoticeRange(), "org")) {
                LambdaQueryWrapper<BizOrg> lambdaQueryWrapper = new LambdaQueryWrapper<>();
                lambdaQueryWrapper.like(BizOrg::getName, bizNoticeGetParam.getSearchName()).in(BizOrg::getId, loginUserDataScope);
                List<BizOrg> list = bizOrgService.list(lambdaQueryWrapper);
                list.forEach(item -> {
                    BizNoticeList bizNoticeList = new BizNoticeList(item);
                    re.add(bizNoticeList);
                });
            }
            if (Objects.equals(bizNoticeGetParam.getNoticeRange(), "user")) {
                LambdaQueryWrapper<BizUser> lambdaQueryWrapper = new LambdaQueryWrapper<>();
                lambdaQueryWrapper.like(BizUser::getName, bizNoticeGetParam.getSearchName()).in(BizUser::getOrgId, loginUserDataScope);
                List<BizUser> list = bizUserService.list(lambdaQueryWrapper);
                list.forEach(item -> {
                    BizNoticeList bizNoticeList = new BizNoticeList(item);
                    re.add(bizNoticeList);
                });
            }
            if (Objects.equals(bizNoticeGetParam.getNoticeRange(), "group")) {
                LambdaQueryWrapper<BizGroupUser> lambdaQueryWrapper = new LambdaQueryWrapper<>();
                lambdaQueryWrapper.like(BizGroupUser::getGroupName, bizNoticeGetParam.getSearchName()).eq(BizGroupUser::getUserId, bizNoticeGetParam.getUserId());
                List<BizGroupUser> list = bizGroupUserService.list(lambdaQueryWrapper);
                list.forEach(item -> {
                    BizNoticeList bizNoticeList = new BizNoticeList(item);
                    re.add(bizNoticeList);
                });
            }
        }
        return re;
    }

    @Override
    public void sendMessage(BizNoticeMessageParam bizNoticeMessageParam) {
        // 查询通知对象(并封装)
        // 封装通知对象
    }
}
